

package vistas;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import controladores.CCategoria;
import controladores.COrden;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class VOrden extends javax.swing.JFrame {
	private COrden ord;
	
    private DefaultTableModel modeloTabla;
    private Vector<String> fila;
   
    public VOrden(COrden orden) {
    	super();
		initComponents();
		ord= orden;
        modeloTabla= new DefaultTableModel();
        fila= new Vector<String>();

        tblProdcuto.setModel(modeloTabla);
        fila.add("Producto");
        fila.addElement("Precio");
        fila.add("Cantidad");
        fila.add("Sub-Total");
        modeloTabla.setColumnIdentifiers(fila);
        txtTotal.setEditable(false);
    }


     public void addListener(ActionListener escuchador){
        btnSalir.addActionListener(escuchador);
        btnRegistrar.addActionListener(escuchador);
        btnCancelar.addActionListener(escuchador);
        btnAgregar.addActionListener(escuchador);
        btnQuitar.addActionListener(escuchador);
        btnDeshacer.addActionListener(escuchador);
        btnSalir.setActionCommand("Salir");
        btnAgregar.setActionCommand("Agregar");
        btnCancelar.setActionCommand("Cancelar");
        btnQuitar.setActionCommand("Quitar");
        btnRegistrar.setActionCommand("Registrar");
        btnDeshacer.setActionCommand("Deshacer");
    }

     public Integer getCantidad() {
        return (Integer) spnCantidad.getValue();
    }
    public boolean actualizarFila(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbProducto.getItemAt(cmbProducto.getSelectedIndex()))){
                int c = getCantidad();
                double sub = Double.parseDouble(""+modeloTabla.getValueAt(i, 1))*c;
                modeloTabla.setValueAt(""+sub, i, 3);
                modeloTabla.setValueAt(""+c, i, 2);
                return true;
            }
        return false;
    }


    public void agregarFila(String[] v){
        float total;
        if(!txtTotal.getText().isEmpty())
            total = Float.parseFloat(txtTotal.getText());
        else
            total=0;
        if(!actualizarFila()){
            int c = getCantidad();
            fila= new Vector<String>();
            fila.add(v[0]);
            fila.add(v[1]);
            fila.add(""+c);
            float sub = (Float.parseFloat(v[1])*c);
            fila.add("" + sub);
            total+=sub;
            txtTotal.setText(""+total);
            modeloTabla.addRow(fila);
        }
    }    
     

    public void calcularTotal(){
        float total =0;
        for (int i = 0; i < modeloTabla.getRowCount(); i++) 
            total+=Float.parseFloat(""+modeloTabla.getValueAt(i, 3));
        txtTotal.setText(""+total);
    }

    public void quitar(){
        int i = tblProdcuto.getSelectedRow();
        if(i != -1){
            float t = Float.parseFloat(txtTotal.getText())-Float.parseFloat((String) modeloTabla.getValueAt(i, 3));
            modeloTabla.removeRow(i);
            txtTotal.setText(""+t);
        }

        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar el regitstro que desea quitar","Comida Rapida",JOptionPane.ERROR_MESSAGE);
    }

 public void setCodigo(String codigo)
 
 {
	 txtCodigo.setText(codigo);
 }
 
 public void setNombre(String nombre)
 
 {
	 txtNombre.setText(nombre);
 }
 
public void setTotal(float total)
{
	txtTotal.setText(String.valueOf(total));
}
     public void reiniciar(){
         txtCodigo.setText("");
         txtNombre.setText("");
         txtTotal.setText("");
         spnCantidad.setValue(0);
         cmbProducto.setSelectedIndex(0);
        modeloTabla.setRowCount(0);
     }

    public void cargarComboProductos(Vector<String> descripciones){
        cmbProducto.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbProducto.addItem(descripciones.elementAt(i));

        }
    }


    public ArrayList<JTextField> getTextos() {
        ArrayList<JTextField> v = new ArrayList<JTextField>();
        v.add(txtCodigo);
        v.add(txtNombre);
        return v;
    }


    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtNombre() {
        return txtNombre;
    }

    public JTextField getTxtTotal() {
        return txtTotal;
    }

    
    public String getTextoCmbProducto() {
        return (String) cmbProducto.getItemAt(cmbProducto.getSelectedIndex());
    }

    public ArrayList<String []> getInfoTabla(){
          ArrayList<String []> v = new ArrayList<String[]>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++){
        	
            String [] s = new String [] {""+modeloTabla.getValueAt(i, 0),""+modeloTabla.getValueAt(i, 2)};
            v.add(s);
         }
         return v;
    }


    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtTotal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProdcuto = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        cmbProducto = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        spnCantidad = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnAgregar = new javax.swing.JButton();
        btnQuitar = new javax.swing.JButton();
        txtNombre = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnSalir = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnRegistrar = new javax.swing.JButton();
        btnDeshacer = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ordenes");
        getContentPane().setLayout(null);

        txtTotal.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtTotal);
        txtTotal.setBounds(430, 490, 110, 30);
        txtTotal.setBackground(new java.awt.Color(168,217,237));

        jLabel5.setFont(new java.awt.Font("Verdana", 0, 15));
        jLabel5.setText("Total:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(370, 490, 55, 30);

        tblProdcuto.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblProdcuto);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(20, 320, 520, 160);
        jScrollPane1.setBackground(new java.awt.Color(168,217,237));

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel2.setLayout(null);

        jLabel11.setFont(new java.awt.Font("Ubuntu", 1, 15));
        jLabel11.setText("Ingredientes");
        jPanel2.add(jLabel11);
        jLabel11.setBounds(420, 270, 110, 30);

        jPanel2.add(cmbProducto);
        cmbProducto.setBounds(30, 70, 170, 30);

        jLabel9.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel9.setText("Selecione");
        jPanel2.add(jLabel9);
        jLabel9.setBounds(40, 30, 170, 30);

        jLabel10.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel10.setText("Cantidad");
        jPanel2.add(jLabel10);
        jLabel10.setBounds(220, 30, 130, 30);
        jPanel2.add(spnCantidad);
        spnCantidad.setBounds(220, 70, 60, 28);

        jLabel4.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel4.setText("Categoria:");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(110, 280, 140, 32);

        jLabel1.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel1.setText("Productos");
        jPanel2.add(jLabel1);
        jLabel1.setBounds(110, 0, 140, 32);

        btnAgregar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnAgregar.setText("Agregar");
        jPanel2.add(btnAgregar);
        btnAgregar.setBounds(30, 120, 120, 40);

        btnQuitar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnQuitar.setText("Quitar");
        jPanel2.add(btnQuitar);
        btnQuitar.setBounds(160, 120, 120, 40);

        getContentPane().add(jPanel2);
        jPanel2.setBounds(120, 130, 310, 180);
        jPanel2.setBackground(new java.awt.Color(168,217,237));

        txtNombre.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtNombre);
        txtNombre.setBounds(240, 80, 200, 32);

        jLabel2.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel2.setText("Nombre Cliente:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(100, 80, 140, 32);

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        getContentPane().add(txtCodigo);
        txtCodigo.setBounds(240, 30, 200, 30);

        jLabel6.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        jLabel6.setText("Codigo:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(100, 30, 100, 30);

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(null);

        btnSalir.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnSalir.setText("Salir");
        jPanel3.add(btnSalir);
        btnSalir.setBounds(270, 10, 112, 40);

        btnCancelar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnCancelar);
        btnCancelar.setBounds(140, 10, 120, 40);

        btnRegistrar.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        jPanel3.add(btnRegistrar);
        jPanel3.add(getBtnDeshacer());
        btnRegistrar.setBounds(10, 10, 120, 40);


        btnDeshacer.setFont(new java.awt.Font("Andale Mono", 0, 15)); // NOI18N
        btnDeshacer.setText("Deshacer");
        jPanel3.add(btnDeshacer);
        jPanel3.add(getBtnDeshacer());
        btnDeshacer.setBounds(387, 12, 113, 38);
        
        getContentPane().add(jPanel3);
        jPanel3.setBounds(20, 532, 512, 60);
        jPanel3.setBackground(new java.awt.Color(168,217,237));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds(0, 0, 560, 663);
        getContentPane().setBackground(new java.awt.Color(230,238,250));
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed

  

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnQuitar;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnDeshacer;
    private javax.swing.JComboBox cmbProducto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spnCantidad;
    private javax.swing.JTable tblProdcuto;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    public String [] getRegistro() {
          String [] s = new String [] {""+cmbProducto.getItemAt(cmbProducto.getSelectedIndex()),""+getCantidad()};

         return s;
    }
    
    private JButton getBtnDeshacer() {
    	if(btnDeshacer == null) {
    		btnDeshacer = new JButton();
    		btnDeshacer.setText("Deshacer");
    		btnDeshacer.setBounds(401, 12, 90, 38);
    	}
    	return btnDeshacer;
    }

}
